<?php

/**
 * @file ReportController
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Report_ReportController extends Zend_Controller_Action {

    // Filtrar: Ejecutivo, Creativo, Clientes
    // Filtro de fechas
    // Nro de orden entregadas
    protected $_PENDING = 0;
    protected $_PROGRESS = 1;
    protected $_REVIEWING = 2;
    protected $_REVISED = 3;
    protected $_DENY = 4;
    protected $_COMPLET = 5;
    protected $_SEND_EMAIL_NEW_ORDER = 0;
    protected $_SEND_EMAIL_CHANGE_ORDER = 1;
    protected $_SEND_EMAIL_DENY_ORDER = 2;

    /* Report main all agency */

    public function indexAction() {
        
    }

    public function getReportAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $auth = Zend_Auth::getInstance();
        $iduser = $auth->getIdentity()->id;

        $ordenDb = new Orden_Model_DbTable_Orden();

        $select = $ordenDb->select()->setIntegrityCheck(false)
                        ->from(array('ou' => 'orden_user'), array('id_user'))->where('ou.id_user=?', $iduser)
                        ->join(array('o' => 'orden'), 'o.id = ou.id_orden', array('status', 'nro_ordenes' => 'COUNT(*)'))
                        ->where('o.status = 0')->orWhere('o.status = 1')->orWhere('o.status = 5')->group('status');

        $dataOr = $ordenDb->fetchAll($select)->toArray();

        $merge = array(
           0 =>  array('id_user' => $iduser, 'status' => 0, 'nro_ordenes' => 0),
           1 => array('id_user' => $iduser, 'status' => 1, 'nro_ordenes' => 0),
           5 => array('id_user' => $iduser, 'status' => 5, 'nro_ordenes' => 0)
        );
        
        $aStatusCurrent = array();
        foreach ($dataOr as $value) {
            $aStatusCurrent[] = $value['status'];
        }
        $aStatus = array(0, 1, 5);
        
        foreach ($aStatus as $value) {
            if(!in_array($value, $aStatusCurrent)){
               $dataOr[] = $merge[$value];
            }
        }  

        $jsonData = Zend_Json::encode($dataOr);
        echo $jsonData;
        exit;
    }

    public function getReportCustomAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $auth = Zend_Auth::getInstance();
        $iduser = $auth->getIdentity()->id;

        if ($this->_request->isXmlHttpRequest()) {
            $ordenDb = new Orden_Model_DbTable_Orden();

            $data = $this->_request->getParams();

            $originalFromDate = $data['from_date'];
            $newFromDate = date("Y-m-d", strtotime($originalFromDate));
            $originalToDate = $data['to_date'];
            $newToDate = date("Y-m-d", strtotime($originalToDate));

            $select = $ordenDb->select()->setIntegrityCheck(false)
                    ->from(array('ou' => 'orden_user'), array('id_user'))->where('ou.id_user=?', $iduser)
                    ->join(array('o' => 'orden'), 'o.id = ou.id_orden', array('status', 'nro_ordenes' => 'COUNT(*)'))
                    ->where('o.create_date > ?', $newFromDate)->where('o.create_date < ?', $newToDate)
                    ->where('o.status = ?', $data['status']);

            $dataOr = $ordenDb->fetchAll($select);
            $jsonData = Zend_Json::encode($dataOr->toArray());
            echo $jsonData;
            exit;
        }
    }

}

?>
